home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: baan.nl!news
- From: avi@green.baan.nl (Avi Cohen Stuart)
- Subject: Re: How do I use abs() on floats ?
- Message-ID: <yqp68cy8n7x.fsf@green.baan.nl>
- Sender: avi@green.baan.nl
- In-Reply-To: timmyd@netcom.com's message of Thu, 22 Feb 1996 20:53:16 GMT
- Date: Fri, 23 Feb 1996 08:23:14 GMT
- X-Nntp-Posting-Host: green.baan.nl
- Reply-To: avi@baan.nl
- References: <4ghki0$b44@LNCSEX0003.eu.btco.com> <timmydDn73Ct.C6r@netcom.com>
- Organization: Baan Development BV
- X-Newsreader: Gnus v5.1
-
- In article <timmydDn73Ct.C6r@netcom.com> timmyd@netcom.com (Tim DeBenedictis) writes:
-
- Use fabs() instead of abs().
-
- -Tim DeBenedictis
- timmyd@netcom.com
-
- PS You could always write your own FABS macro, too; might be faster than
- fabs() but I doubt it:
-
- #define FABS(x) (x>0.0?x:-x)
-
- If you use a macro then do it like this:
-
- #define FABS(x) ((x)>0.0?(x):-(x))
-
- The extra braces will ensure that when you use an expression in combination
- with FABS() that they will be calculated first.
-
- Nice example to go wring:
-
- a = FABS(b+c); expands to:
-
- a = (b+c>0.0?b+c;-b+c);
- ^^^^--------- Wrong!
- while the braced version:
-
- a = ((b+c)>0.0?(b+x):-(b+c));
-
- will do what you expect.
-
- Avi.
-